<?php

namespace App\Http\Controllers\FeeFormula;

use App\Http\Controllers\pagniateable;
use App\Models\AssetBundle;
use App\Models\Fee\FeeFormula;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FeeFormulaController extends Controller
{
    use pagniateable;
    public function list()
    {
        $query = FeeFormula::select("*");
        return $this->paginate($query);
    }

    public function createOrUpdate()
    {
        $_formula = request()->get('feeFormula');
        $isNew = empty($_formula['id']);
        if($isNew) {
            $feeFormula = new FeeFormula();
        }else {
            $feeFormula = FeeFormula::findOrFail($_formula['id']);
        }
        $feeFormula->name = $_formula['name'];
        $feeFormula->description = $_formula['description'];
        $feeFormula->criteria = $_formula['criteria'];
        $feeFormula->formula = $_formula['formula'];
        $feeFormula->save();
        return response()->json(compact(['feeFormula']));
    }

    public function del($id)
    {
        FeeFormula::findOrFail($id)->delete();
        $result = 'ok';
        return response()->json(compact(['result']));
    }
}
